package com.hpch.business.controller;

import cn.hutool.core.util.StrUtil;
import com.hpch.business.util.RoleUtil;
import com.hpch.common.core.web.BaseController;
import com.hpch.business.service.ProductService;
import com.hpch.business.entity.Product;
import com.hpch.business.param.ProductParam;
import com.hpch.common.core.web.ApiResult;
import com.hpch.common.core.web.PageResult;
import com.hpch.common.core.web.PageParam;
import com.hpch.common.core.web.BatchParam;
import com.hpch.common.core.annotation.OperationLog;
import com.hpch.common.system.entity.Role;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 企业产品表控制器
 *
 * @author Lly
 * @since 2025-03-05 21:16:05
 */
@Api(tags = "企业产品表管理")
@RestController
@RequestMapping("/api/business/product")
public class ProductController extends BaseController {
    @Resource
    private ProductService productService;

    @PreAuthorize("hasAuthority('business:product:list')")
    @OperationLog
    @ApiOperation("分页查询企业产品表")
    @GetMapping("/page")
    public ApiResult<PageResult<Product>> page(ProductParam param) {
        PageParam<Product, ProductParam> page = new PageParam<>(param);
        //page.setDefaultOrder("create_time desc");
//        return success(productService.page(page, page.getWrapper()));
        // 如果是管理员登录
        if (RoleUtil.isCompany(getLoginUser().getRoles())) {
            param.setUserId(getLoginUserId());
        }
        // 使用关联查询
        return success(productService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('business:product:list')")
    @OperationLog
    @ApiOperation("查询全部企业产品表")
    @GetMapping()
    public ApiResult<List<Product>> list(ProductParam param) {
        PageParam<Product, ProductParam> page = new PageParam<>(param);
        //page.setDefaultOrder("create_time desc");
//        return success(productService.list(page.getOrderWrapper()));
        // 使用关联查询
        if (RoleUtil.isCompany(getLoginUser().getRoles())) {
            param.setUserId(getLoginUserId());
        }
        return success(productService.listRel(param));
    }

    @PreAuthorize("hasAuthority('business:product:list')")
    @OperationLog
    @ApiOperation("根据id查询企业产品表")
    @GetMapping("/{id}")
    public ApiResult<Product> get(@PathVariable("id") Integer id) {
//        return success(productService.getById(id));
        // 使用关联查询
        return success(productService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('business:product:save')")
    @OperationLog
    @ApiOperation("添加企业产品表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody Product product) {

        if (StrUtil.hasBlank(product.getTitle(), product.getThumb())) {
            return fail("标题或缩略图不能为空");
        }

        List<Role> roles = getLoginUser().getRoles();
        if (RoleUtil.isDevAdmin(roles) || RoleUtil.isSurAdmin(roles)) {
            if (product.getUserId() == null) {
                return fail("请选择所属企业");
            }
        }

        if (productService.edit(getLoginUser(), product)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('business:product:update')")
    @OperationLog
    @ApiOperation("修改企业产品表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody Product product) {

        if (product.getId() == null) {
            return fail("id不能为空");
        }

        if (StrUtil.hasBlank(product.getTitle(), product.getThumb())) {
            return fail("标题或缩略图不能为空");
        }

        List<Role> roles = getLoginUser().getRoles();
        if (RoleUtil.isDevAdmin(roles) || RoleUtil.isSurAdmin(roles)) {
            if (product.getUserId() == null) {
                return fail("请选择所属企业");
            }
        }

        if (productService.edit(getLoginUser(), product)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }


    /**
     * 修改状态
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('business:product:update')")
    @OperationLog
    @ApiOperation("修改企业产品表状态")
    @GetMapping("/status")
    public ApiResult<?> updateStatus(Integer id) {
        if (productService.updateStatus(id)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('business:product:remove')")
    @OperationLog
    @ApiOperation("删除企业产品表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (productService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('business:product:save')")
    @OperationLog
    @ApiOperation("批量添加企业产品表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<Product> list) {
        if (productService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('business:product:update')")
    @OperationLog
    @ApiOperation("批量修改企业产品表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<Product> batchParam) {
        if (batchParam.update(productService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('business:product:remove')")
    @OperationLog
    @ApiOperation("批量删除企业产品表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (productService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
